home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Value_Input.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-20  |  3.7 KB  |  132 lines

  1. //
  2. // "$Id: Fl_Value_Input.cxx,v 1.6.2.1 1999/05/20 18:01:41 bill Exp $"
  3. //
  4. // Value input widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Fltk widget for drag-adjusting a floating point value.
  27. // Warning: this works by making a child Fl_Input object, even
  28. // though this object is *not* an Fl_Group.  May be a kludge?
  29.  
  30. #include <FL/Fl.H>
  31. #include <FL/Fl_Value_Input.H>
  32. #include <FL/Fl_Group.H>
  33. #include <stdlib.h>
  34.  
  35. static Fl_Value_Input* hack_o_rama;
  36.  
  37. void Fl_Value_Input::input_cb(Fl_Widget*, void* v) {
  38.   Fl_Value_Input& t = *(Fl_Value_Input*)v;
  39.   double nv;
  40.   if (t.step()>=1.0) nv = strtol(t.input.value(), 0, 0);
  41.   else nv = strtod(t.input.value(), 0);
  42.   hack_o_rama = &t;
  43.   t.handle_push();
  44.   t.handle_drag(nv);
  45.   t.handle_release();
  46.   hack_o_rama = 0;
  47. }
  48.  
  49. void Fl_Value_Input::draw() {
  50.   if (damage()&~FL_DAMAGE_CHILD) input.clear_damage(FL_DAMAGE_ALL);
  51.   input.box(box());
  52.   input.color(color(), selection_color());
  53.   input.draw();
  54.   input.clear_damage();
  55. }
  56.  
  57. void Fl_Value_Input::resize(int X, int Y, int W, int H) {
  58.   Fl_Valuator::resize(X, Y, W, H);
  59.   input.resize(X, Y, W, H);
  60. }
  61.  
  62. void Fl_Value_Input::value_damage() {
  63.   if (hack_o_rama==this) return;
  64.   char buf[128];
  65.   format(buf);
  66.   input.value(buf);
  67.   input.mark(input.position()); // turn off selection highlight
  68. }
  69.  
  70. int Fl_Value_Input::handle(int event) {
  71.   double v;
  72.   int delta;
  73.   int mx = Fl::event_x();
  74.   static int ix, drag;
  75.   switch (event) {
  76.   case FL_PUSH:
  77.     if (!step()) goto DEFAULT;
  78.     ix = mx;
  79.     drag = Fl::event_button();
  80.     handle_push();
  81.     return 1;
  82.   case FL_DRAG:
  83.     if (!step()) goto DEFAULT;
  84.     delta = Fl::event_x()-ix;
  85.     if (delta > 5) delta -= 5;
  86.     else if (delta < -5) delta += 5;
  87.     else delta = 0;
  88.     switch (drag) {
  89.     case 3: v = increment(previous_value(), delta*100); break;
  90.     case 2: v = increment(previous_value(), delta*10); break;
  91.     default:v = increment(previous_value(), delta); break;
  92.     }
  93.     v = round(v);
  94.     handle_drag(soft()?softclamp(v):clamp(v));;
  95.     return 1;
  96.   case FL_RELEASE:
  97.     if (!step()) goto DEFAULT;
  98.     if (value() != previous_value() || !Fl::event_is_click())
  99.       handle_release();
  100.     else {
  101.       input.handle(FL_PUSH);
  102.       input.handle(FL_RELEASE);
  103.     }
  104.     return 1;
  105.   case FL_FOCUS:
  106.     return input.take_focus();
  107.   default:
  108.   DEFAULT:
  109.     input.type(step()>=1.0 ? FL_INT_INPUT : FL_FLOAT_INPUT);
  110.     return input.handle(event);
  111.   }
  112. }
  113.  
  114. Fl_Value_Input::Fl_Value_Input(int x, int y, int w, int h, const char* l)
  115. : Fl_Valuator(x, y, w, h, l), input(x, y, w, h, 0) {
  116.   soft_ = 0;
  117.   if (input.parent())  // defeat automatic-add
  118.     ((Fl_Group*)input.parent())->remove(input);
  119.   input.parent(this); // kludge!
  120.   input.callback(input_cb, this);
  121.   input.when(FL_WHEN_CHANGED);
  122.   box(input.box());
  123.   color(input.color());
  124.   selection_color(input.selection_color());
  125.   align(FL_ALIGN_LEFT);
  126.   value_damage();
  127. }
  128.  
  129. //
  130. // End of "$Id: Fl_Value_Input.cxx,v 1.6.2.1 1999/05/20 18:01:41 bill Exp $".
  131. //
  132.